"""
项目配置文件
"""
import os
from dotenv import load_dotenv


# 加载环境变量
load_dotenv()


class config:
    """基础配置"""
    # MySQL数据库配置
    MYSQL_HOST = os.getenv("MYSQL_HOST", "localhost")
    MYSQL_PORT = int(os.getenv("MYSQL_PORT", 3306))
    MYSQL_USER = os.getenv("MYSQL_USER", "root")
    MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "123456")
    MYSQL_DATABASE = os.getenv("MYSQL_DATABASE", "xiang")
    
    # 数据库连接URL
    SQLALCHEMY_DATABASE_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8mb4"
    
    # JWT配置
    JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "your-secret-key-change-in-production")
    JWT_ALGORITHM = "HS256"
    JWT_ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24  # 24小时
    
    # Redis 配置
    REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
    REDIS_PORT = int(os.getenv("REDIS_PORT", 6379))
    REDIS_DB = int(os.getenv("REDIS_DB", 0))
    REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", None)
    REDIS_KEY_PREFIX = os.getenv("REDIS_KEY_PREFIX", "netease:auth:")
    # 与 JWT 对齐的 Token 过期时间（秒）
    REDIS_TOKEN_TTL_SECONDS = int(os.getenv(
        "REDIS_TOKEN_TTL_SECONDS",
        JWT_ACCESS_TOKEN_EXPIRE_MINUTES * 60,
    ))

    # 钉钉 OAuth 配置
    DINGTALK_CLIENT_ID: str = "dingc4ndq3d0mj7pkzgg"
    DINGTALK_CLIENT_SECRET: str = (
        "iQMYfCaT2C1XtTug5sn7Ddir_KMSSD34uX3SfbVufhIJOkYa-Ex-7EwoVM_aVPRV"
    )
    DINGTALK_REDIRECT_URI: str = "http://localhost:5173/dingding-callback"
    
    # 应用配置
    APP_NAME = "房屋租赁登录系统"
    APP_VERSION = "1.0.0"
    
    # 七牛云配置
    QINIU_ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY", "")
    QINIU_SECRET_KEY = os.getenv("QINIU_SECRET_KEY", "")
    QINIU_BUCKET_NAME = os.getenv("QINIU_BUCKET_NAME", "")
    QINIU_DOMAIN = os.getenv("QINIU_DOMAIN", "")
    
    # 支付宝配置
    ALIPAY_APP_ID = os.getenv("ALIPAY_APP_ID", "9021000157661754")
    ALIPAY_NOTIFY_URL = os.getenv("ALIPAY_NOTIFY_URL", "http://localhost:8000/api/payment/alipay/notify")
    ALIPAY_RETURN_URL = os.getenv("ALIPAY_RETURN_URL", "http://localhost:8000/api/payment/alipay/return")
    ALIPAY_DEBUG = os.getenv("ALIPAY_DEBUG", "True").lower() == "true"
    # 支付宝网关地址（新版本沙箱环境使用新地址）
    # 沙箱环境：https://openapi-sandbox.dl.alipaydev.com/gateway.do
    # 旧沙箱环境：https://openapi.alipaydev.com/gateway.do
    # 生产环境：https://openapi.alipay.com/gateway.do
    ALIPAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do" if ALIPAY_DEBUG else "https://openapi.alipay.com/gateway.do"
    
    # Celery配置
    CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL", "redis://localhost:6379/1")
    CELERY_RESULT_BACKEND = os.getenv("CELERY_RESULT_BACKEND", "redis://localhost:6379/2")



